﻿/******************************************************************************
 * GNewsTypes.cs
 * 
 * This module implements the DataContract types needed to query Google News
 * 
 * Date:   1/2009
 * 
 * Copyright (c) 2009, Mark Betz 
 * 
 * All rights reserved. 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 
 *   * Redistributions of source code must retain the above copyright 
 *     notice, this list of conditions and the following disclaimer. 
 *   * Redistributions in binary form must reproduce the above copyright 
 *     notice, this list of conditions and the following disclaimer in the 
 *     documentation and/or other materials provided with the distribution. 
 *   * Neither the name of the Author nor the names of contributors may be 
 *     used to endorse or promote products derived from this software
 *     without specific prior written permission. 
 *     
 * THIS SOFTWARE IS PROVIDED BY MARK BETZ ''AS IS'' AND ANY  EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL MARK BETZ BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 * 
 */
using System;
using System.Runtime.Serialization;
using System.IO;
using System.Diagnostics;

namespace GSearch
{
    /// <summary>
    /// Implements the relatedStories json object. This is a subset of
    /// the full returned information for a story, and is used to embed
    /// related story information in results.
    /// </summary>
    [DataContract(Name = "relatedStory")]
    public class GNewsRelated
    {
        /// <summary>
        /// A System.String containing the title/headline of the news
        /// story. May contain HTML formatting.
        /// </summary>
        [DataMember(Name = "title")]
        public string Title { get; set; }

        /// <summary>
        /// A System.String containing the title/headline of the news
        /// story, without any HTML formatting.
        /// </summary>
        [DataMember(Name = "titleNoFormatting")]
        public string TitleNoFormatting { get; set; }

        /// <summary>
        /// A System.String containing the URL of the original news story,
        /// without escape characters.
        /// </summary>
        [DataMember(Name = "unescapedUrl")]
        public string UnescapedUrl { get; set; }

        /// <summary>
        /// A System.String containing the URL of the original news story,
        /// with escape characters.
        /// </summary>
        [DataMember(Name = "url")]
        public string Url { get; set; }

        /// <summary>
        /// A System.String containing the publisher of the news story. This
        /// is typically the name of the newspaper or website.
        /// </summary>
        [DataMember(Name = "publisher")]
        public string Publisher { get; set; }

        /// <summary>
        /// A System.String containing the location reference for the news
        /// story. This is often in the form city,state,country.
        /// </summary>
        [DataMember(Name = "location")]
        public string Location { get; set; }

        /// <summary>
        /// A System.String containing the publication date for the news
        /// story.
        /// </summary>
        [DataMember(Name = "publishedDate")]
        public string PublishedDate { get; set; }
    }
    
    /// <summary>
    /// Implements the image json object. This is used to embed a referenced
    /// image in a news story.
    /// </summary>
    [DataContract(Name = "image")]
    public class GNewsImage
    {
        /// <summary>
        /// A System.String containing the title/caption of the image. May
        /// contain HTML formatting.
        /// </summary>
        [DataMember(Name = "title")]
        public string Title { get; set; }

        /// <summary>
        /// A System.String containing the title/caption of the image,
        /// without HTML formatting.
        /// </summary>
        [DataMember(Name = "titleNoFormatting")]
        public string TitleNoFormatting { get; set; }

        /// <summary>
        /// A System.String containing the URL address of the original image.
        /// </summary>
        [DataMember(Name = "url")]
        public string Url { get; set; }

        /// <summary>
        /// A System.String containing the name of the publisher of the image.
        /// </summary>
        [DataMember(Name = "publisher")]
        public string Publisher { get; set; }

        /// <summary>
        /// A System.String containing the URL address of a thumbnail version
        /// of the image.
        /// </summary>
        [DataMember(Name = "tbUrl")]
        public string TbUrl { get; set; }

        /// <summary>
        /// A System.Int32 containing the width of the thumbnail image in
        /// pixels.
        /// </summary>
        [DataMember(Name = "tbWidth")]
        public int TbWidth { get; set; }

        /// <summary>
        /// A System.Int32 containing the height of the thumbnail image in
        /// pixels.
        /// </summary>
        [DataMember(Name = "tbHeight")]
        public int TbHeight { get; set; }
    }
    
    /// <summary>
    /// Implements the news-specific portion of the Google result json object
    /// </summary>
    [DataContract]

    public class GNewsResults : GResults
    {
        public override string ToString()
        {
            return Title;
        }
        /// <summary>
        /// A System.String containing the title/headline of the news
        /// story. May contain HTML formatting.
        /// </summary>
        [DataMember(Name = "title")]
        public string Title { get; set; }

        /// <summary>
        /// A System.String containing the title/headline of the news
        /// story, without any HTML formatting.
        /// </summary>
        [DataMember(Name = "titleNoFormatting")]
        public string TitleNoFormatting { get; set; }

        /// <summary>
        /// A System.String containing the URL of the original news story,
        /// without escape characters.
        /// </summary>
        [DataMember(Name = "unescapedUrl")]
        public string UnescapedUrl { get; set; }

        /// <summary>
        /// A System.String containing the URL of the original news story,
        /// with escape characters.
        /// </summary>
        [DataMember(Name = "url")]
        public string Url { get; set; }

        /// <summary>
        /// A System.String containing the URL of a "cluster page" with links
        /// to stories which are related to the news story.
        /// </summary>
        [DataMember(Name = "clusterUrl")]
        public string ClusterUrl { get; set; }
        
        /// <summary>
        /// A System.String containing an HTML-formatted snippet of content
        /// from the news story.
        /// </summary>
        [DataMember(Name = "content")]
        public string Content { get; set; }

        /// <summary>
        /// A System.String containing the name of the publisher of the news story.
        /// This is typically the newspaper or website name.
        /// </summary>
        [DataMember(Name = "publisher")]
        public string Publisher { get; set; }

        /// <summary>
        /// A System.String containing the location reference for the news
        /// story. This is often in the form city,state,country.
        /// </summary>
        [DataMember(Name = "location")]
        public string Location { get; set; }

        /// <summary>
        /// A System.String containing the publication date for the news
        /// story.
        /// </summary>
        [DataMember(Name = "publishedDate")]
        public string PublishedDate { get; set; }
        
        /// <summary>
        /// An array of <see cref="GSearch.GNewsRelated"/> objects containing
        /// information about related stories. Optional, may not be present
        /// for all news stories.
        /// </summary>
        [DataMember(Name = "relatedStories", IsRequired = false)]
        public GNewsRelated[] RelatedStories { get; set; }
        
        /// <summary>
        /// An instance of a <see cref="GSearch.GNewsImage"/> object containing
        /// information about an image related to the news story. Optional,
        /// may not be present for all news stories.
        /// </summary>
        [DataMember(Name = "image", IsRequired = false)]
        public GNewsImage Image { get; set; }

        /// <summary>
        /// A System.String containing the language in which the new story
        /// is written. Optional, may not be present for all news stories.
        /// </summary>
        [DataMember(Name = "language", IsRequired = false)]
        public string Language { get; set; }
    }

    /// <summary>
    /// Implements the news-specific portion of the Google responseData json object
    /// </summary>
    [DataContract]
    public class GNewsResponseData : GResponseData
    {
        /// <summary>
        /// Returns an array of <see cref="GSearch.GNewsResults"/> objects containing
        /// the results of a search.
        /// </summary>
        [DataMember(Name = "results")]
        public GNewsResults[] Results { get; set; }
    }

    /// <summary>
    /// Implements the news-specific portion of the Google response json object
    /// </summary>
    [DataContract]
    public class GNewsResponse : GResponse
    {
        /// <summary>
        /// An instance of <see cref="GSearch.GNewsResponseData"/> containing the
        /// results of a search.
        /// </summary>
        [DataMember(Name = "responseData")]
        public GNewsResponseData Response { get; set; }
    }
}